<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class RAR
{


    function mkdirs($path){
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
        var_dump($path); exit;
    }

   public function decompression($packrar,$extractTO){

        $basePath = basename($packrar,'.rar');

        $rar = rar_open($packrar) or die("获取资源失败");

        if(rar_broken_is($rar)){//如果损坏的话也允许打开

            rar_allow_broken_set(true);

        }

        if(rar_solid_is($rar)) die("压缩包内容为空");

        $entry_list = rar_list($rar) or die("获取条目失败");

        if($entry_list){

             $i=0;
            foreach ($entry_list as $entry) {
                $name = $this->entryName($entry->getName());
                $name= iconv('GBK', 'utf-8//IGNORE',$name);
                $dir =  is_dir($name) ? $dir : dirname($name);//在获取的entry对象中既有文件也要文件夹
                $file = $extractTO.$i.".jpg";
                if(!$entry->isDirectory()){//判读是否为目录
                    $stream = $entry->getStream();
                    $this->freadEntry($stream,$file);
                    fclose($stream);
                    $i++;
                }

            }

        }

        rar_close($rar);

        return true;



    }

    function entryName($name){

        return mb_convert_encoding(htmlentities($name,ENT_COMPAT,"UTF-8"),"HTML-ENTITIES","UTF-8");

    }

    /**

     *解压

     */

    function freadEntry($str,$path){

        if($str){
            $fw = fopen($path, "w+");
            while (!feof($str)) {
                $buffer = @fread($str,1024);
                if($buffer){
                    fwrite($fw, $buffer);
                }
            }
            fclose($fw);

        }

    }

}